# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=keycloak
pkgver=25.0.6
pkgrel=1
pkgdesc="Open source identity and access management for modern applications and services"
url="https://www.keycloak.org"
# aarch64: blocked by outdated @ast-grep/napi
# armhf,armv7,ppc64le,riscv64,x86: blocked by openjdk17
# s390x: blocked by java-netty-transport-native
# loongarch64: blocked by maven
arch="noarch !aarch64 !armhf !armv7 !ppc64le !riscv64 !s390x !x86 !loongarch64"
license="Apache-2.0"
depends="
	java-jre-headless
	java-netty-transport-native
	"
makedepends="
	bash
	maven
	nodejs
	npm
	java-jdk
	"
pkgusers="keycloak"
pkggroups="keycloak"
install="$pkgname.pre-install"
subpackages="$pkgname-openrc"
source="https://github.com/keycloak/keycloak/archive/$pkgver/keycloak-$pkgver.tar.gz
	support-for-readonly-usr-share.patch
	remove-deps.patch
	keycloak.conf
	$pkgname.initd
	$pkgname.confd
	$pkgname.logrotated
	"
# net: fetches dependencies from Maven repositories
options="!check net"

# NOTE: Maven 3 dropped support for the M2_HOME variable.
export MAVEN_ARGS="--batch-mode -Duser.home=$srcdir/.home"

prepare() {
	default_prepare

	# Workaround for frontend-maven-plugin to use system-provided node and npm.
	# https://github.com/eirslett/frontend-maven-plugin/issues/1089
	mkdir -p node
	cp -r /usr/lib/node_modules node/
	ln -s /usr/bin/node node/
	ln -s /usr/bin/npm node/
	ln -s /usr/bin/npx node/
	ln -s ../node js/
}

build() {
	# NOTE: -Dskip.installnodenpm doesn't work, frontend-maven-plugin always
	#  downloads prebuilt node and npm, unless it's already present in the
	#  ./node directory and the version matches.
	local mvn_opts="-DskipTests -Dnode.version=$(node --version)"

	cd quarkus
	mvn -f ../pom.xml install $mvn_opts -DskipExamples -DskipTestsuite
	mvn install $mvn_opts
	tar -C dist -xf dist/target/keycloak-*.tar.gz
}

package() {
	cd quarkus/dist/keycloak-*

	local destdir="$pkgdir/usr/share/$pkgname"
	local builddir="$(pwd)"

	mkdir -p "$destdir"
	cp -r bin lib providers themes version.txt "$destdir"/
	rm -f "$destdir"/bin/*.bat
	rm -f "$destdir"/bin/*.orig

	cd "$pkgdir"

	mkdir -p usr/bin
	local i; for i in kc kcadm kcreg; do
		ln -s ../share/$pkgname/bin/$i.sh usr/bin/$i
	done

	install -d -m755 -o keycloak -g keycloak \
		var/lib/$pkgname
	install -d -m750 -o keycloak -g keycloak \
		var/lib/$pkgname/data \
		var/log/$pkgname

	mv "$destdir"/lib/quarkus "$destdir"/lib/quarkus.dist
	ln -s /var/lib/$pkgname/lib/quarkus "$destdir"/lib/quarkus

	install -d -m750 -o root -g keycloak etc/$pkgname
	install -D -m640 -o keycloak -g keycloak -t etc/keycloak/ \
		"$builddir"/conf/cache-ispn.xml \
		"$srcdir"/keycloak.conf

	install -D -m755 "$srcdir"/$pkgname.initd etc/init.d/$pkgname
	install -D -m644 "$srcdir"/$pkgname.confd etc/conf.d/$pkgname
	install -D -m644 "$srcdir"/$pkgname.logrotated etc/logrotate.d/$pkgname
}

sha512sums="
7593545e993c045c76db12048a27c158105cf85bffd1fe5ba7739478769409ba784aa1aadebc040656d00e8e524a1c67929279e0086fa820f8fa3c87c6f9023f  keycloak-25.0.6.tar.gz
50c8bd0395e36d9be94225e3f4cf63d9c64bf063c954af2584962f66c96b90a648ff0d6d65b080b81157294cc456cb7418e6c0f197ea05a0b09c5382479b27e5  support-for-readonly-usr-share.patch
2b6e93af7de2af532eb9465c742124ef86aded6975d9ee3bb8f0803cc5bc61b9d1f74858cb8bdf2a2814003f617b36e858cda6cfe6bc6a5ee530fba22d85a63e  remove-deps.patch
c434d72ffa991d25d099cdec40f266f9f1c24ab7fc6b3b40a8d0d55c5df5ad8160cc7241b8c42e2a432978ca00e45aafc544564b7d8cefa207d0f6d9a1c5b7f9  keycloak.conf
66523332c22e21681f1b51f88bdd36d61b46c332a2e0a68db6f1e2f67216a0e1cfe66adfb8b9cad300eb43216a98804e6537bdc874713966cc4d8c03d95ee9f1  keycloak.initd
46b8aa4ba438cbf1d59871b9dbdf849edc9a3247fd49a84b70d191025ef8cbd666d9c64e7586cd39af975228efe97bcfed4c4f6d15ef07de3fbafc6e526406d7  keycloak.confd
5fe5972a71c630e7335f6a73b10d7258dcbb0448f3a9dab2aac94fe9a4905c0cbb5ebbc4c74113696296e19a207fd019c72430ff53feab7255edce91ed89fd11  keycloak.logrotated
"
